/**
 * Created by yunyang on 13-10-28.
 */

$(function(){
    var data  = getFileList();
    showData(data);
});


/**
 *同步AJAX获取数据
 */
function getFileList(){
    var data = null;
    $.ajax({
        url : '/fileList',
        type : 'get',
        data : {uid : $("#uid").val()},
        "async":false,
        success : function(result){
            data = result;
        },
        error : function(result){
            data = [];
        }
    });
    return data;
}


function showData(data){
    var columns = [{
        label: '上传时间',
        width: '30%',
        formatter:function(row){
            var date = new Date(row.createdate  * 1);
            return date.toLocaleString();
        }
    }, {
        label: '文件名称',
        width: '30%',
        formatter:function(row){
            return row.filename;
        }
    }, {
        label: '文件大小',
        width: "25%",
        formatter:function(row){
            return (row.size * 1)/ 1000 + "M";
        }
    },{
        label: '操作',
        width: '15%',
        formatter:function(row){
            return "<a href='/download?file="+encodeURIComponent(row.path) +"&fileName="+encodeURIComponent(row.filename)+"'>下载</a>";
        }
    }];

    //定义分页控件
    var page = $("#page").paging({
        pageSize: 13,
        currentPage: 1
    });

    //定义数据源
    var dataSource = new DataSource({
        columns: columns,
        getDataFunction: function (begin, ps) {
            //远程数据调用
            var result = data;
            return result;
        }
    });

    // 实例化grid控件
    var grid = $('#grid').grid({
        theme: 'theme-record',
        dataSource: dataSource,
        paging: page,
        hideHeader: false,
        rowStyleEnable: true
    });
}

function refresh(){
    location.href = "/list?uid=" +$("#uid").val();
}